package com.runfast.paotui.task.async;

import cn.jpush.api.push.PushResult;
import com.runfast.paotui.dao.model.Role;
import com.runfast.paotui.dao.model.RunfastCuser;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.dao.model.RunfastShopperExample;
import com.runfast.paotui.mq.AmqpClient;
import com.runfast.paotui.service.RunfastCuserService;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.paotui.utils.JiGuangPushUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author: lijin
 * @date: 2018年04月13日
 */

@Async
@Component
@Slf4j
public class MessagePushTask {

    @Autowired
    private RunfastShopperService shopperService;

    @Autowired
    private RunfastCuserService cuserService;

    @Autowired
    private AmqpClient amqpClient;


    public void newOrderNotify(String orderNo, Integer agentNearById) {
        RunfastShopperExample shopperExample = new RunfastShopperExample();
        shopperExample.createCriteria().andAgentIdEqualTo(agentNearById);
        List<RunfastShopper> shoppers = shopperService.selectByExample(shopperExample);

        HashMap<String, String> params = new HashMap<>();
        params.put("orderNo", orderNo);
        ArrayList<String> aliases = new ArrayList();
        for (RunfastShopper shopper : shoppers) {

            if (StringUtils.isNotBlank(shopper.getAlias()))
                aliases.add(shopper.getAlias());
        }
        if (aliases.isEmpty()) return;
        PushResult pushResult = JiGuangPushUtil.push(Role.Predefine.DRIVER, 600, aliases, "您有新的跑腿订单" + orderNo, params, "neworder.caf");
        if (!pushResult.isResultOK()) {

            StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
            log.error("文件名：" + stackTraceElement.getFileName()
                    + " 类和方法名：" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()
                    + " 行号：" + stackTraceElement.getLineNumber()
                    + " 描述：" + pushResult.getOriginalContent());
        }

    }


    public  void takenOrderNotity(String orderNo, Integer cuserId) {

        RunfastCuser cuser = cuserService.selectByPrimaryKey(cuserId);
        String alias = cuser.getAlias();
        if (StringUtils.isNotBlank(alias)) {
            HashMap<String, String> params = new HashMap<>();
            params.put("orderNo", orderNo);
            PushResult pushResult = JiGuangPushUtil.push(Role.Predefine.DRIVER, 600, alias, "您的跑腿订单" + orderNo + "骑手已接单", params, null);
            if (!pushResult.isResultOK()) {

                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[1];
                log.error("文件名：" + stackTraceElement.getFileName()
                        + " 类和方法名：" + stackTraceElement.getClassName() + "." + stackTraceElement.getMethodName()
                        + " 行号：" + stackTraceElement.getLineNumber()
                        + " 描述：" + pushResult.getOriginalContent());
            }
        }


    }


}
